{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bbdbfa7c-fe68-436f-8f57-47cfc3927134",
   "metadata": {},
   "source": [
    "## 模型包装器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc4f01c8-0779-444c-b637-ebffe6de45e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import FakeListLLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a5f30d3c-6ac2-48e4-b201-072fee998986",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通用的模型包装器\n",
    "llm = FakeListLLM(responses=['样例输出'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bfaf573c-15a8-4883-a508-49e2b846bd0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'样例输出'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm.invoke('test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af4ec288-c507-46b5-abb5-17668aae8c07",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 聊天模型包装器\n",
    "from langchain.schema import (\n",
    "  AIMessage,\n",
    "  HumanMessage,\n",
    "  SystemMessage\n",
    ")\n",
    "from langchain.chat_models import FakeListChatModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8f672070-32c7-4651-b1c9-57ed1fbdf179",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = FakeListChatModel(responses=['样例输出'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "66276e00-259c-4951-8670-e4d0f45a1490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='样例输出')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages = [\n",
    "    SystemMessage(content=\"你是取名大师，你擅长取名\"),\n",
    "    HumanMessage(content=\"帮我公司取名，名字包含 YDD\")\n",
    "]\n",
    "chat.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "896f16ed-2539-4d6a-ae2e-793d313d7e94",
   "metadata": {},
   "source": [
    "## 提示词模板"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43ce0cb1-aa3a-4e52-9eb0-421f7e09ec04",
   "metadata": {},
   "source": [
    "### PromptTemplate 使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ae7de3c5-2a50-47e7-a6a4-c816f0d5f06a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3044ca31-51c6-44c3-8147-defd80ca23ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你是一个专业的程序员，很懂 k8s 领域知识。解释一下Deployment'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "template = '你是一个专业的程序员，很懂 k8s 领域知识。解释一下{resource}'\n",
    "# 实例化方式一\n",
    "prompt = PromptTemplate(template=template, input_variables=['resource'])\n",
    "# 实例化方式二\n",
    "prompt = PromptTemplate.from_template(template)\n",
    "prompt.format(resource='Deployment')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f272e53-1bd0-4db1-880d-6040961738de",
   "metadata": {},
   "source": [
    "### ChatPromptTemplate 使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3bba8dba-44fb-4cbb-a6a8-e5b7303f358a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.prompts import (\n",
    "    AIMessagePromptTemplate,\n",
    "    HumanMessagePromptTemplate,\n",
    "    SystemMessagePromptTemplate,\n",
    "    ChatPromptTemplate\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7856d4cf-7db7-4bb0-8430-41ca622ff83c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=['resource'], messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='你是一个专业的程序员，很懂 k8s 领域知识')), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['resource'], template='解释一下{resource}'))])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "system_prompt = '你是一个专业的程序员，很懂 k8s 领域知识'\n",
    "system_prompt_tep = SystemMessagePromptTemplate.from_template(system_prompt)\n",
    "human_prompt = '解释一下{resource}'\n",
    "human_prompt_tep = HumanMessagePromptTemplate.from_template(human_prompt)\n",
    "chat_prompt_tep = ChatPromptTemplate.from_messages([system_prompt_tep, human_prompt_tep])\n",
    "chat_prompt_tep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4b2d3f31-2394-42f1-b2c6-f291793daade",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='你是一个专业的程序员，很懂 k8s 领域知识'), HumanMessage(content='解释一下test')])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt_tep.format_prompt(resource=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "46e245c9-f418-42d4-a19c-d5fd00990228",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='你是一个专业的程序员，很懂 k8s 领域知识'),\n",
       " HumanMessage(content='解释一下test')]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt_tep.format_prompt(resource=\"test\").to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2a104949-d75c-4e6e-9a8e-8cec44db8bfa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'System: 你是一个专业的程序员，很懂 k8s 领域知识\\nHuman: 解释一下test'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt_tep.format_prompt(resource=\"test\").to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faa97c36-abff-4b90-9fed-a4f6763a7502",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
